<?php
namespace app\index\controller;

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\Controller;
use think\Db;

class Base extends Controller
{
    public function _initialize()
    {
        global $domain;
        $domain = request()->domain();
        $domain = str_replace("http://", "", $domain);
        $cms = model("cms");
        $cms = $cms->get(function ($query) {
            global $domain;
            $domainarr = explode(".", $domain);
            $query->where('domain', $domain)->whereOr('username', $domainarr[0]);
        });
        if ($cms == null) {
            exit("I cannot found domain");
        }
        $this->cms = $cms;
        $this->cms_id = $cms['id'];
        $this->username = $this->cms['username'];
        $this->from = request()->isMobile() && $this->cms->hasmobile == "1" ? "mobile" : "pc";
        $this->assign("qiniuprefix", config("qiniu.prefix"));
        $this->assign("css", "/home/{$this->username}/{$this->from}/css");
        $this->assign("js", "/home/{$this->username}/{$this->from}/js");
        $this->assign("img", "/home/{$this->username}/{$this->from}/images");
       /* $_cms=cache("cms_".$this->cms_id);
        if($_cms==null)
        {
            $_cms=$this->get();
            cache("cms_".$this->cms_id,$_cms);
        }*/
        $this->cms=$_cms=$this->get();

        $this->assign("cms",$_cms);
    }

    private function get()
    {
        $info =toarray( model("info")->where("cms_id", $this->cms_id)->find());
        $navlist = toarray(model("nav")->where("cms_id", $this->cms_id)->where("upid", 0)->order("sort asc,id asc")->select());
        $slidelist = toarray(model("slide")->where("cms_id", $this->cms_id)->order("sort asc,id asc")->select());
        $_articlelist = model("article")->where("cms_id", $this->cms_id)->where("alias", "not null")->where("alias", "<>", "")->select();
        $articlehref = [];
        foreach ($_articlelist as $_article) {
            $articlehref[$_article['alias']] = $_article['id'];
        }
        $catelist = [];
        $_catelist = model("cate")->where("cms_id", $this->cms_id)->where("alias", "not null")->where("alias", "<>", "")->select();
        foreach ($_catelist as $_cate) {
            $catelist[$_cate['alias']] = ["name" => $_cate['name'], "id" => $_cate['id'], "sublist" => []];
            if ($_cate['upid'] == "0") {
                $subcount = model("cate")->where("upid", $_cate['id'])->count();
                if ($subcount != 0) {
                    $_subcatelist = model("cate")->where("cms_id", $this->cms_id)->where("upid", $_cate['id'])->select();
                    foreach ($_subcatelist as $_subcate) {
                        $subcate = ["name" => $_subcate['name'], "id" => $_subcate['id']];
                        $catelist[$_cate['alias']]['sublist'][]=$subcate;
                    }
                }
            }

        }
        foreach($catelist as $key=>$_cate){
            $catelist[$key]['list']=$this->getcatelist($_cate['id']);
            for($i=0;$i<count($catelist[$key]['sublist']);$i++){
                $catelist[$key]['sublist'][$i]['list']=$this->getcatelist($catelist[$key]['sublist'][$i]['id']);
            }
        }

        return ["info"=>$info,"navlist"=>$navlist,"articlehref"=>$articlehref,"catelist"=>$catelist,"slidelist"=>$slidelist];

    }
    private function getcatelist($cate_id){
        $list=model("article")->field("title,id,cms_id,thumb,des,publish_time")->where("cms_id", $this->cms_id)->where("cate_id",$cate_id)->order("sort desc,id desc")->limit(16)->select();
        return $list;
    }


    public function dislpay($name = null)
    {
        return $this->view->config('view_path', "./home/{$this->username}/{$this->from}/html/")->fetch($name);
    }


}
